/*
 * Copyright (c) 2024 Black Sesame Technologies
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef _DAL_FASTDDS_SERVICE_REQ_READER_LISTENER_IMPL_HPP_
#define _DAL_FASTDDS_SERVICE_REQ_READER_LISTENER_IMPL_HPP_

#include <mutex>
#include "dal/service.h"
#include <fastdds/dds/subscriber/Subscriber.hpp>
#include <fastdds/dds/subscriber/DataReader.hpp>
#include <fastdds/dds/subscriber/DataReaderListener.hpp>

using namespace eprosima::fastdds::dds;

class DalServiceImpl;

class ServiceReqReaderListenerImpl : public DataReaderListener
{
public:
    explicit ServiceReqReaderListenerImpl(DalServiceImpl* dal_service_impl);

    void on_data_available(DataReader* reader);
    void on_subscription_matched(DataReader* reader, const SubscriptionMatchedStatus& info) final;

    size_t get_unread_requests();
    void set_on_new_request_callback(dal_service_on_request_t callback, const void* user_data);

private:
    DalServiceImpl* dal_service_impl_;
    dal_service_on_request_t on_req_cb_{nullptr};
    const void* user_data_{nullptr};
    std::mutex on_new_req_mutex_;
};

#endif  // _DAL_FASTDDS_SERVICE_REQ_READER_LISTENER_IMPL_HPP_